/*
 * @FilePath: \undefinedc:\Users\sxjct\.leetcode\47.全排列-ii.cpp
 * @Brief: 
 * @Version: 1.0
 * @Date: 2021-03-22 15:37:02
 * @Author: tianyiyi
 * @Copyright: Copyright@tianyiyi
 * @LastEditors: Mr.Tian
 * @LastEditTime: 2021-03-22 16:01:59
 */
/*
 * @lc app=leetcode.cn id=47 lang=cpp
 *
 * [47] 全排列 II
 */

// @lc code=start
class Solution {
    private:
    vector<vector<int>> res;
public:
    vector<vector<int>> permuteUnique(vector<int>& nums) {
        vector<int> path;
        vector<bool> used(nums.size(),0);
        sort(nums.begin(),nums.end());
        backtrack(nums,path,used);
        return res;
    }

    void backtrack(vector<int> &nums,vector<int> &path,vector<bool> &used)
    {
        if(nums.size() == path.size())
        {
            res.push_back(path);
            return ;
        }

        for(int i = 0; i < nums.size() ; i++)
        {
            if(!used[i])
            {
                if(i > 0 && nums[i] == nums[i - 1]&& !used[i-1])
                {
                    continue;    
                }
                path.push_back(nums[i]);
                used[i] = 1;
                backtrack(nums,path,used);
                path.pop_back();
                used[i] = 0;
            }
        }
    }
};
// @lc code=end

